package com.ccsoft.atools.service;

import com.ccsoft.atools.dao.ContactCompanyMapper;
import com.ccsoft.atools.model.ContactCompany;
import com.ccsoft.atools.utils.BusinessException;
import com.sun.prism.impl.Disposer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ContactCompanyServiceImpl implements ContactCompanyService {

    @Autowired
    private ContactCompanyMapper dao;

    @Override
    public int deleteByPrimaryKey(Integer iid) {
        return dao.deleteByPrimaryKey(iid);
    }

    @Override
    public ContactCompany insert(ContactCompany record) {
        ContactCompany re = dao.selectBySid(record.getSid());
        if (re != null)
            throw new BusinessException("编号" + re.getSid() + "的往来单位已经存在！");

        int ret = dao.insert(record);
        return record;
    }

    @Override
    public ContactCompany insertSelective(ContactCompany record) {
        System.out.println("传入的即将被保存的对象是：" + record.toString());
        ContactCompany re = dao.selectBySid(record.getSid());
        if (re != null)
            throw new BusinessException("编号" + re.getSid() + "的往来单位已经存在！");
        int ret = dao.insertSelective(record);

        System.out.println("保存后返回的对象是：" + record.toString());
        return record;
    }

    @Override
    public ContactCompany selectByPrimaryKey(Integer iid) {
        return dao.selectByPrimaryKey(iid);
    }

    @Override
    public ContactCompany updateByPrimaryKeySelective(ContactCompany record) {
        ContactCompany re = dao.selectBySid(record.getSid());
        if (re != null)
            throw new BusinessException("编号" + re.getSid() + "的往来单位已经存在！");

        int ret = dao.updateByPrimaryKeySelective(record);
        ContactCompany result = selectByPrimaryKey(record.getIid());
        return result;
    }

    @Override
    public ContactCompany updateByPrimaryKey(ContactCompany record) {
//        ContactCompany re = dao.selectBySid(record.getSid());
//        if( re != null )
//            throw new BusinessException("编号" + re.getSid() + "的往来单位已经存在！");

        int ret = dao.updateByPrimaryKey(record);
        ContactCompany result = dao.selectByPrimaryKey(record.getIid());
        return result;
    }

    @Override
    public List<ContactCompany> selectAll() {
        return dao.selectAll();
    }

    @Override
    public ContactCompany selectBySid(String sid) {
        return dao.selectBySid(sid);
    }

    @Override
    public boolean isExist(String sid) {
        ContactCompany record = dao.selectBySid(sid);
        return record == null ? false : true;
    }

    @Override
    public List<ContactCompany> selectByType(String typeSid) {
        return dao.selectByType(typeSid);
    }

}
